﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ToolHelp
{
    public static class HttpRestHelper
    {
        public async static Task<T> PostJson<T>(string url, object body) where T : class,new()
        {
            try
            {
                var client = new RestClient(url);
                var request = new RestRequest();
                request.Method = Method.Post;
                request.AddJsonBody(body.ToJson());
                var response = await client.PostAsync(request);
                var result = response.Content.ToObject<T>();
                return result;
            }
            catch (Exception ex)
            {
                SerilLogHelper.ErrorLog($"请求出错:URL:{url} 方法:HttpRestHelper.PostJson", ex);
                return null;
            }
        }
        public async static Task<T> PostParam<T>(string url,Dictionary<string,object> dic) where T : class, new()
        {
            try
            {
                var client = new RestClient(url);
                var request = new RestRequest();
                request.Method = Method.Post;
                if (dic != null && dic.Count > 0)
                {
                    foreach (var item in dic.Keys)
                    {
                        request.AddParameter(item, dic[item], ParameterType.QueryString);
                    }
                }
                var response = await client.PostAsync(request);
                var result = response.Content.ToObject<T>();
                return result;
            }
            catch (Exception ex)
            {
                SerilLogHelper.ErrorLog($"请求出错:URL:{url} 方法:HttpRestHelper.PostParam", ex);
                return null;
            }
        }
        public async static Task<T> GetJson<T>(string url, object body) where T : class, new()
        {
            try
            {
                var client = new RestClient(url);
                var request = new RestRequest();
                request.Method = Method.Get;
                request.AddJsonBody(body.ToJson());
                var response = await client.GetAsync(request);
                var result = response.Content.ToObject<T>();
                return result;
            }
            catch (Exception ex)
            {
                SerilLogHelper.ErrorLog($"请求出错:URL:{url} 方法:HttpRestHelper.GetJson", ex);
                return null;
            }
        }
        public async static Task<T> GetParam<T>(string url,Dictionary<string, object> dic) where T : class
        {
            try
            {
                var client = new RestClient(url);
                var request = new RestRequest();
                request.Method = Method.Get;
                if (dic != null && dic.Count > 0)
                {
                    foreach (var item in dic.Keys)
                    {
                        request.AddParameter(item, dic[item], ParameterType.QueryString);
                    }
                }
                var response = await client.GetAsync(request);
                var result = response.Content.ToObject<T>();
                return result;
            }
            catch (Exception ex)
            {
                SerilLogHelper.ErrorLog($"请求出错:URL:{url} 方法:HttpRestHelper.GetParam", ex);
                return null;
            }
        }
    }
}
